#include "PhysicsWorld.h"
#include "NeroEngine\NMemoryManager.h"

PhysicsWorld::PhysicsWorld(void)
	:mWorld(NULL)
{
	InitWorld();
}

PhysicsWorld::~PhysicsWorld(void)
{
	DeinitWorld();
}

void PhysicsWorld::InitWorld()
{
	// Define the gravity vector.
	b2Vec2 gravity(0.0f, GRAVITY);		

	//Create Physics World
	mWorld = MY_NEW(Memory::HID_Physics,"PhysicsWorld") b2World(gravity);	
	mWorld->SetContactListener(&mListener);
}

void PhysicsWorld::DeinitWorld()
{
	MY_DELETE(mWorld);
}

b2World* PhysicsWorld::GetWorld()
{
	return mWorld;
}

b2Body* PhysicsWorld::CreateBody(b2BodyDef* bodydef)
{
	return mWorld->CreateBody(bodydef);
}

void PhysicsWorld::Update(const Game::NTime& gameTime)
{
	float32 timeStep = 1.0f / 60.0f;
	int32 velocityIterations = 6;
	int32 positionIterations = 2;

	/* 
		Instruct the world to perform a single step of simulation.
		It is generally best to keep the time step and iterations fixed.
	*/
	mWorld->Step(timeStep, velocityIterations, positionIterations);
}

float PhysicsWorld::ConvertWorldToScreen(float value)
{
	return value * CONVERSION;
}

float PhysicsWorld::ConvertScreenToWorld(float value)
{
	return value / CONVERSION;
}